<?php

/**
 * SSO单点登录
 * File Name：SSOServer.php
 * File Encoding：UTF-8
 * File New Time：2014-10-21 16:22:08
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class SSOServer extends YzwController {

    //不使用布局
    public $layout = false;
    //站点id
    protected $client_id = 0;
    //站点ip
    protected $client_ip = '127.0.0.1';
    //接收数据
    protected $data = array();
    //应用列表
    protected $apps = array(
        //www.1zw.com 预留
        1 => array(
            'url' => 'http://www.1zw.com/sso/api/index.html', //api通知地址
            'client_id' => 1, //站点id
            'client_key' => 'FIv2v8ivfUY6WUGIfzKr7Vfn4L7qQ5VFVGvF4NtF3Dg0Hx3Hy2SoR3', //站点密钥
            'key_validity' => 3000, //密文有效期，单位秒
            'synlogin' => 1, //是否开启同步登录退出
        ),
        //妈咪街www.mamijie.com
        2 => array(
            'url' => 'http://www.mamijie.com/api/user/index.html', //api通知地址
            'client_id' => 2, //站点id
            'client_key' => 'Kr7Vg0Hx3HyKr7Vg0Hx3Hy2So5Zyb09BbLkFIv2So5Zyb09BbLkFIv', //站点密钥
            'key_validity' => 3000, //密文有效期，单位秒
            'synlogin' => 1, //是否开启同步登录退出
        ),
        //bbs.1zw.com
        3 => array(
            'url' => 'http://bbs.1zw.com/yzw_ssoapi.php', //api通知地址
            'client_id' => 3, //站点id
            'client_key' => 'Kr7Vg0Hx3Hy2So5Zyb09BbLkFIv2v8ivfUY6WUG2Av6Om8Fw2Dw9Dr0Ay7', //站点密钥
            'key_validity' => 3000, //密文有效期，单位秒
            'synlogin' => 1, //是否开启同步登录退出
        ),
    );

    //初始化
    public function init() {
        parent::init();
        if (!IS_POST) {
            throw new CException('SSO Request Error');
        }
        $this->client_ip = $_POST['client_ip'];
        $this->client_id = $_POST['client_id'];
        if (empty($this->client_id) || empty($this->apps[$this->client_id])) {
            $this->error('该站点非法！');
        }
        parse_str(Encrypt::authcode($_POST['data'], 'DECODE', $this->apps[$this->client_id]['client_key']), $data);
        if (empty($data)) {
            $this->error('该站点非法！');
        }
        $this->data = $data;
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param string $message 错误信息
     * @param string $jumpUrl 页面跳转地址
     * @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
     * @return void
     */
    protected function error($message = '', $jumpUrl = '', $ajax = true) {
        $this->dispatchJump($message, 0, $jumpUrl, $ajax);
    }

    /**
     * 成功返回数据
     * @param type $return
     */
    protected function returnData($return = '') {
        $data = array();
        $data['status'] = true;
        if (!empty($return)) {
            $data['data'] = $return;
        }
        $this->ajaxReturn($data);
    }

}
